###
# @module JsBuilder::Documentor
#
module JsBuilder
        
    ###
    # @class Documentor
    #
    class Documentor
        
        #JSDOC_VERSION = '2.0a'    #1.4.1b
        JSDOC_VERSION = '2.0a'
        
        ###
        # path to jsdoc toolkit
        # should be specified in a config file probably.
        #
        #JS_DOC_TOOLKIT_DIR = "#{File.dirname(__FILE__)}/jsdoc_toolkit-#{JSDOC_VERSION}"
        JS_DOC_TOOLKIT_DIR = "#{File.dirname(__FILE__)}/jsdoc-toolkit-2.0a-svn"
        
        
        ###
        # @constructor
        #
        def initialize(project)                
            if (!cfg = JsBuilder.load_config)
                puts "Error JsBuilder::Documenter -- Could not load config file"
                exit(0)
            end
                                                                                       
            if (cfg[project.to_sym].nil?)
                puts "Error JsBuild::Documentor -- Could not locate that project in config file"
                exit(0)
            end            
            @config = cfg[project.to_sym]                            
        end
        
        ###
        # build
        # build the shit.
        #
        def build
            puts " * JsBuilder::Documentor::build"               
            
            output_dir = @config[:doc_dir]
            output_dir.sub!(/\$path/, "#{RAILS_ROOT}/#{@config[:path]}")
            output_dir.sub!(/\$output/, "#{RAILS_ROOT}/#{@config[:output]}")
            source_dir = "#{RAILS_ROOT}/#{@config[:path]}"                                    
            
            if (JSDOC_VERSION == '2.0a')
                # -d=#{output_dir}
                template = 'ext'
                cmd = "java -jar jsrun.jar app/main.js -a -t=templates/#{template} #{source_dir} -d=#{output_dir} -r"
    
            elsif (JSDOC_VERSION == 'svn')
                template = 'ext'            
                #-t=templates/#{template}
                cmd = "java -jar app/js.jar app/run.js -a  #{source_dir} -d=#{output_dir}"    
            else
                # build the command.
                cmd = "java -jar #{JS_DOC_TOOLKIT_DIR}/app/js.jar #{JS_DOC_TOOLKIT_DIR}/app/run.js -d=#{output_dir} -a -t=templates/sunny  #{source_dir} -r"            
            end
            
            # show the command to user before executing
            puts cmd
            
            puts "******************************************************************"
            puts "* JsBuilder::Documentor -- Building documentation please wait..."
            puts "* JSDOC_VERSION: #{JSDOC_VERSION}"
            puts "# JS_DOC_TOOLKIT_DIR: #{JS_DOC_TOOLKIT_DIR}"
            puts "******************************************************************"
                        
            # execute cmd.  have to do a "cd" command as well.
            puts `cd #{JS_DOC_TOOLKIT_DIR} \n #{cmd}`                                                
                       
        end
    end
        
end
